package qq

import (
	"api-go/cache"
	"api-go/serializer"
	"api-go/util"
	"encoding/json"
	"fmt"
	"strconv"
	"time"

	"github.com/go-redis/redis/v8"
)

// 歌单所需信息
type PlayList struct {
	Category string `form:"category"`
	Limit    string `form:"limit"`
	Offset   string `form:"offset"`
	Id       string `form:"id"`
}

//获取歌单列表
func (service *PlayList) PlaylistList() *serializer.Response {

	if val := service.Category; val != "" {
		service.Category = val
	} else {
		service.Category = "10000000"
	}

	if val := service.Limit; val != "" {
		service.Limit = val
	} else {
		service.Limit = "20"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}
	//字符串转数字，用于下面的计算
	newLimit, _ := strconv.ParseInt(service.Limit, 10, 64)
	newOffset, _ := strconv.ParseInt(service.Offset, 10, 64)
	sin := newLimit * (newOffset - 1)
	ein := newOffset*newLimit - 1

	url := "https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg?picmid=1&g_tk=732560869&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8" +
		"&notice=0&platform=yqq.json&needNewCode=0" +
		"&categoryId=" + service.Category + "&sortId=5&sin=" + strconv.FormatInt(sin, 10) + "&ein=" + strconv.FormatInt(ein, 10)

	fmt.Println(url)
	res, err := util.QQGet(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}

//获取歌单信息
func (service *PlayList) PlaylistInfo() *serializer.Response {

	if val := service.Id; val != "" {
		service.Id = val
	} else {
		service.Id = "7675374821"
	}

	url := "http://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&utf8=1&disstid=" + service.Id + "&loginUin=0&format=json"

	res, err := util.QQGet(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}

//获取歌单分类
func (service *PlayList) PlaylistCat() *serializer.Response {
	//定义redis key和判断redis是否有该值
	redisKey := "qq-playlist-cat"
	redisResult, err := cache.RedisClient.Get(ctx, redisKey).Result()
	//若redis没有该值则执行获取
	if err == redis.Nil {
		url := "https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_tag_conf.fcg?format=json&inCharset=utf8&outCharset=utf-8"

		res, err := util.QQGet(url)
		if err != nil {
			return err
		}

		//将结果转为Json数据
		b, e := json.Marshal(res)
		if e != nil {
			fmt.Println(err)
		}
		//转为string存入redis
		e = cache.RedisClient.Set(ctx, redisKey, string(b), 24*time.Hour).Err()
		if e != nil {
			fmt.Println("err1", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}

		return serializer.OkResponse(res)
	} else if err != nil {
		fmt.Println("err2", err)
		return serializer.ErrorResponse(serializer.RedisError)
	} else {
		//将从redis获取的结果（string类型）转为map[string]inerface{}
		bodyMapData := map[string]interface{}{}
		err = json.Unmarshal([]byte(redisResult), &bodyMapData)
		if err != nil {
			fmt.Println("err3", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}
		return serializer.OkResponse(bodyMapData)
	}

}
